<?php
namespace app\hsycms\controller;
use think\Controller;
use app\hsycms\controller\Base;

class Tpl extends Base
{
	
	public $htmlTplRoot; //模板根目录
	public $skinTplRoot; //资源根目录
	
	public function __construct(){
		parent::__construct();
		$this->htmlTplRoot = ROOT_PATH . 'app/index/view';
		$this->skinTplRoot = ROOT_PATH . 'static';
	}
	
	
	//模板列表
	public function index() {
		$list = db('tpl')->order("status,type")->select();
		$this->assign("list",$list);
		return $this->fetch(); 
	}
	
	//添加模板
	public function add(){
		$id = input('id','','intval');
		if($_POST){
			$data = input('post.');
			if($id){
				$w['entitle'] = $data['entitle'];
				$w['id'] = array('neq',$id);
				if(db('tpl')->where($w)->find()){ 
					return json(['status'=>'no','info'=>"模板已存在"]);
				}
				if(db('tpl')->where('id',$id)->update($data)!==false){
					return json(['status'=>'y','info'=>"修改成功"]);
				}
			}else{
				if(db('tpl')->where('entitle',$data['entitle'])->find()){
					return json(['status'=>'no','info'=>"模板已存在"]);
				}
				db('tpl')->insert($data);
				return json(['status'=>'y','info'=>"添加成功"]);
			}
		}else{
			if($id){
				$one = db('tpl')->where('id',$id)->find();
				$this->assign("v",$one);
			}
			return $this->fetch();
		}
	}
	
	//删除模板
	public function del(){
		$id = input('id','','intval');
		if($id){
			db('tpl')->where(['id'=>$id,'status'=>2])->delete();
			return json(['status'=>'y','info'=>"删除成功"]);
		}
	}
	
	//启用
	public function optStatus(){
		$id = input('id','','intval');
		$type = input('type','','intval');
		if($id){
			$themes = db('tpl')->where('id',$id)->value("entitle");
			$tpldir = $this->htmlTplRoot .'/'. $themes;			
			if(file_exists($tpldir)){
				db('tpl')->where('type',$type)->update(['status'=>2]);
				db('tpl')->where(['id'=>$id])->update(['status'=>1,'type'=>$type]);
				if($type==1){ //PC
					db('config')->where(['name'=>'webthemes'])->update(['value'=>$themes]);
				}else if($type==2){ //手机
					db('config')->where(['name'=>'webmthemes'])->update(['value'=>$themes]);
				}
				$this->success("启用成功");
			}else{				
				$this->error("启用失败，模板不存在!");
			}
		}
	}	
	
	//模板文件
	public function fileindex(){
		$id      = input('id','','intval');
		$type    = input('type',1,'intval');
		$file 	 = input('file');
		if($file==""){
			$file = db('tpl')->where('id',$id)->value("entitle");			
			$dir = $type == 1 ? $this->htmlTplRoot .'/'. $file : $this->skinTplRoot .'/'. $file;		
		}else{
			$dir = $file;
			$this->assign("file",$file);
		}
		//取得当前文件所在目录
		$list = getFileDir($dir);		
		foreach($list as $key=>$v){
			if($type==1){
				$list[$key]['src'] = substr($v['file'],strpos($v['file'],"view")+4);
			}else{
				$list[$key]['src'] = substr($v['file'],strpos($v['file'],"static")+6);
			}
		}		
		$this->assign("list",$list);
		$this->assign("id",$id);
		$this->assign("dir",$dir);
		$this->assign("type",$type);
		return $this->fetch('file'); 
	}
	
	//编辑文件
	public function acefile(){
		$file = input('file');
		$type = input('type');
		$dir = $type == 1 ? $this->htmlTplRoot . $file : $this->skinTplRoot . $file;
		if (!file_exists($dir)) {
		    return json(['status' => 'no', 'info' => "可编辑文件不存在".$dir]);
		}
		$ext = pathinfo($dir, PATHINFO_EXTENSION);		
		if (!in_array($ext, ['html', 'css', 'js'])) {
		    return json(['status' => 'no', 'info' => '可编辑文件后缀异常'.$dir]);
		}
		if(request()->isAjax()){
				$filecontent = input('filecontent');
				$mgc = ['eval','exec','scandir','shell_exec','ini_set','ini_restore','popen'];
				foreach ($mgc as $k => $v) {
						if (strpos($filecontent, $v) !== false) {
							return json(['status' => 'no', 'info' => '编辑内容存在危险关键词：'.$v.'，请检查后重新提交']);
						}
				}
				file_put_contents($dir, $filecontent);
				return json(['status' => 'y', 'info' => '已保存']);
		}
		$filecontent = file_get_contents($dir);
		$this->assign(['filecontent'=>htmlspecialchars($filecontent),'type'=>$type, 'file'=>$file,'ext' => $ext]);
		return $this->fetch();
	}
	
	//检查文件是否存在 
	public function exitfile(){
		if(request()->get()){
			$file = input('file');
			$type = input('type');
			$dir = $type == 1 ? $this->htmlTplRoot . $file : $this->skinTplRoot . $file;
			if (!file_exists($dir)) {
					return json(['status' => 'no', 'info' => "可编辑文件不存在"]);
			}
			$ext = pathinfo($dir, PATHINFO_EXTENSION);
			if (!in_array($ext, ['html', 'css', 'js'])) {
					return json(['status' => 'no', 'info' => '可编辑文件后缀异常']);
			}
			return json(['status' => 'y', 'info' => '可编辑']);
		}
	}
	
}